﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class SqlServer : UserControl
    {
        public SqlServer()
        {
            InitializeComponent();
        }


        private void button2_Click(object sender, EventArgs e)
        {

            string zhi = textBox3.Text;
            string zi = textBox4.Text;
            panel1.Controls.Clear();
            List<string> ziList = GetString(zi.Split(Environment.NewLine.ToCharArray()));

            int k = 0;
            int kk = 0;
            for (int i = 0; i < ziList.Count; i++)
            {
                CheckBox CheckBox = new CheckBox();
                CheckBox.Text = ziList[i];
                CheckBox.Name = i.ToString();
                if (i != 0)
                {
                    if ((i * 55) % 275 == 0)
                    {
                        k++;
                        kk = 0;
                    }
                }
                CheckBox.Top = k * 22;
                CheckBox.Checked = true;
                CheckBox.Left = kk * 110;
                panel1.Controls.Add(CheckBox);
                kk++;
            }
        }

        public List<string> GetString(string[] mylist)
        {
            List<string> str = new List<string>();

            foreach (var item in mylist)
            {
                if (!string.IsNullOrWhiteSpace(item))
                {
                    str.Add(item);
                }
            }
            return str;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox5.Text = "";
            //string textBox6 = "\r\n\r\n\r\n";

            string shujuku = textBox1.Text;
            string biao = textBox2.Text;

            if (string.IsNullOrWhiteSpace(biao))
            {
                MessageBox.Show("请填写表名！");
                return;
            }

            if (checkBox2.Checked)
            {
                if (biao.Substring(biao.Length - 1, 1).ToLower() != "y")
                {
                    if (biao.Substring(biao.Length - 1, 1).ToLower() != "s")
                    {
                        biao = biao + "s";
                    }
                }
                else
                {
                    MessageBoxButtons messButton = MessageBoxButtons.OKCancel;
                    DialogResult dr = MessageBox.Show("后字母为Y是否替换为【ies】?", "替换", messButton);

                    if (dr == DialogResult.OK)//如果点击“确定”按钮
                    {
                        biao = biao.Substring(0, biao.Length - 1) + "ies";
                    }

                }
            }

            string zhi = textBox3.Text;
            string zi = textBox4.Text;

            List<string> zhiList = GetString(zhi.Split(Environment.NewLine.ToCharArray()));
            List<string> ziList = GetString(zi.Split(Environment.NewLine.ToCharArray()));

            string sb = "";

            if (!string.IsNullOrWhiteSpace(shujuku))
            {
                sb += "USE " + shujuku + ";\r\n"
                    + " \r\n"
                    + " SET FOREIGN_KEY_CHECKS=0; \r\n"
                    + " \r\n";
            }

            sb += "CREATE TABLE IF NOT EXISTS `" + biao + "`(\r\n";

            //int k = panel1.Controls.Count;
            foreach (Control item in panel1.Controls)
            {
                CheckBox CheckBox = (CheckBox)item;
                if (CheckBox.Checked)
                {
                    string myname = zhiList[int.Parse(item.Name)];

                    if (!string.IsNullOrWhiteSpace(myname))
                    {
                        sb += myname + " COMMENT '" + item.Text + "',\r\n";
                    }

                }
            }

            sb += "PRIMARY KEY (`ID`)\r\n)ENGINE=InnoDB DEFAULT CHARSET=utf8";
            //sb += "CONSTRAINT [PK_"+biao.ToUpper()+"] PRIMARY KEY CLUSTERED([ID] ASC"
            //        +")WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]"
            //        +") ON [PRIMARY]"
            //        +"\r\n";

            string beizhu = "";

            if (string.IsNullOrWhiteSpace(textBox7.Text))
            {
                textBox7.BackColor = Color.Red;
            }
            else
            {
                textBox7.BackColor = Color.White;
                beizhu = " COMMENT='" + textBox7.Text + "';";
                //beizhu = "EXECUTE sp_addextendedproperty N'MS_Description', '" + textBox7.Text + "', N'user', N'dbo', N'table', N'" + biao + "', NULL, NULL";
            }

            textBox5.Text = sb + beizhu + " \r\n\r\n";

        }

        public void Getzifu()
        {
            string zhi = textBox3.Text;
            string zi = textBox4.Text;

            string[] ziList = zhi.Split(Environment.NewLine.ToCharArray());
            string[] zhiList = zi.Split(Environment.NewLine.ToCharArray());
            int k1 = 0; int k2 = 0;
            foreach (string item in zhiList)
            {
                if (!string.IsNullOrWhiteSpace(item))
                {
                    k1++;
                }
            }
            foreach (string item in ziList)
            {
                if (!string.IsNullOrWhiteSpace(item))
                {
                    k2++;
                }
            }

            toolStripStatusLabel1.Text = "字：" + k1.ToString() + "/" + (zhiList.Length / 2 + 1);
            toolStripStatusLabel2.Text = "值：" + k2.ToString() + "/" + (ziList.Length / 2 + 1);
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            foreach (Control item in panel1.Controls)
            {
                CheckBox CheckBox = (CheckBox)item;
                CheckBox.Checked = checkBox1.Checked;
            }
        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {
            Getzifu();
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            Getzifu();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Clipboard.SetDataObject(textBox5.Text);
            if (button3.Text == "复制")
            {
                button3.Text = "复制√";
            }
            else
            {
                button3.Text = "复制";
            }
        }
        
        private void button6_Click(object sender, EventArgs e)
        {
            wofuzhi(0);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            wofuzhi(2);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            wofuzhi(4);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            wofuzhi(6);
        }

        private void button8_Click(object sender, EventArgs e)
        {
            wofuzhi(8);
        }

        private void button9_Click(object sender, EventArgs e)
        {
            wofuzhi(10);
        }
        public void wofuzhi(int shuzi)
        {
            string[] ziList = textBox6.Text.Split(Environment.NewLine.ToCharArray());

            Clipboard.SetDataObject(ziList[shuzi]);

        }


        private void SqlServer_Load(object sender, EventArgs e)
        {
            panel1.AutoScroll = true;
            panel1.VerticalScroll.Value = panel1.VerticalScroll.Maximum;

            Getzifu();
        }
    }
}
